package dao;

import java.security.InvalidKeyException;
import java.security.InvalidParameterException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import model.Contrato;
import model.Divida;
import model.Relatorio;

public class Kernel {

	private Connection connection;
	private static Kernel k;

	private Kernel() throws SQLException {
		connection = DriverManager.getConnection("jdbc:mysql:mysql.cobranca.net:3306", "test", "test");
	}

	public static Kernel getKernel() throws SQLException {
		if (k == null) {
			k = new Kernel();
		}
		return k;
	}

	public int authentication(String user, String pass) throws SQLException,
			InvalidParameterException, InvalidKeyException {
		int a;
		ResultSet rs;

		a = -1;

		rs = query("SELECT type, active FROM users WHERE user = '" + user
				+ "' AND password = MD5('" + pass + "')");

		if (rs.next()) {
			if (rs.getInt(1) != 1) {
				throw new InvalidParameterException("Usuario desativado");
			} else {
				a = rs.getInt(0);
			}
		} else {
			throw new InvalidKeyException("Usuario inexistente");
		}

		return a;
	}

	public void passwordChange(String user, String pass, String userChanged,
			String newPass) throws SQLException, InvalidParameterException,
			InvalidKeyException {
		if (authentication(user, pass) == 1) {
			query("UPDATE users SET password = MD5('" + newPass
					+ "') WHERE user = '" + userChanged + "'");
		} else {
			throw new InvalidParameterException("Voce nao tem permissao");
		}
	}

	private ResultSet query(String q) throws SQLException {
		return connection.createStatement().executeQuery(q);
	}

	public String getPassword(String user) throws SQLException {
		String pw = "";
		ResultSet rs;

		rs = query("SELECT password FROM users WHERE user = '" + user + "'");

		if (rs.next()) {
			pw = rs.getString(0);
		}

		return pw;
	}

	public boolean activation(String user, String pass, String userChanged)
			throws SQLException, InvalidParameterException, InvalidKeyException {
		boolean b;
		if (authentication(user, pass) == 1) {
			query("UPDATE users SET active = 1 WHERE user = '" + userChanged
					+ "'");
			b = true;
		} else {
			throw new InvalidParameterException("Voce nao tem permissao");
		}

		return b;
	}

	public boolean deactivation(String user, String pass, String userChanged)
			throws SQLException, InvalidParameterException, InvalidKeyException {
		boolean b;
		if (authentication(user, pass) == 1) {
			query("UPDATE users SET active = 0 WHERE user = '" + userChanged
					+ "'");
			b = true;
		} else {
			throw new InvalidParameterException("Voce nao tem permissao");
		}

		return b;
	}

	public List<Divida> obterDividas(int cliente) {
		return null;
	}

	public void registrarRelatorio(Relatorio relatorio) {

	}

	public int inserirContrato(float faixaJuros, float faixaHonorarios)
			throws SQLException {
		int id = -1;
		query("INSERT INTO contrato VALUES(null, " + faixaJuros + ", "
				+ faixaHonorarios + ")");
		ResultSet rs;

		rs = query("SELECT id FROM contrato ORDER BY id DESC LIMIT 0, 1");
		if (rs.next()) {
			id = rs.getInt(0);
		}

		return id;
	}

	public Contrato obterContrato(int contrato) throws SQLException {
		ResultSet rs;
		Contrato c;
		c = null;

		rs = query("SELECT * FROM contrato WHERE id = " + contrato);
		if (rs.next()) {
			c = new Contrato(rs.getInt(0), rs.getFloat(1), rs.getFloat(2));
		}

		return c;
	}
}
